global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_4_firmweight_descriptives.log, replace name(tabl)

capture noi {


	* v6
	* Descriptive statistics for ﬁrms in our baseline regression

	texdoc init ${tab_dir}/main/Table_4_firmweight_descriptives.tex, replace
	texdoc stlog, nolog

	* ---------------------- * 
	* Prepare data
	* ---------------------- * 

	* prepare total + foreign weights in long format

	* samples to run this on
	local s1 "auto95"
	
		
	local regsample "`s1'"

	* domestic + foreign
	use BvD lse_id using ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear 
	duplicates drop
	mmerge BvD using ${final_dir}/bvd_list_regfirms_`regsample'.dta, unmatched(master)
	keep if _m == 3
	mmerge BvD using ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_tfacit1.dta, unmatched(master)
	keep lse_id share2_all*
	duplicates drop
	reshape long share2_all_1995_, i(lse_id) j(country, string)
	ren share2_all_1995_ weight
	tempfile totalweights_s1
	save `totalweights_s1', replace

	* foreign only, normalized to 1
	do ${code_dir}/config/country_list.do
	use BvD lse_id country_shr_1995 using ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear 
	duplicates drop
	mmerge BvD using ${final_dir}/bvd_list_regfirms_`regsample'.dta, unmatched(master)
	keep if _m == 3 
	mmerge BvD using ${dataset_dir}/weights/bvdid_pat_weights_EPtr_from1970_1995_orbis2017_tfacit1.dta, unmatched(master)
	keep lse_id share2_all* country_shr_1995
	egen maxshare = rowmax(share2_all_1995_??)
	gen foreign_share = 1 - maxshare
	foreach ctry of global countrylist1995{
	replace share2_all_1995_`ctry' = 0 if maxshare==share2_all_1995_`ctry'
	}
	foreach ctry of global countrylist1995{
		replace share2_all_1995_`ctry' = share2_all_1995_`ctry' / foreign_share
	}
	keep lse_id share2_all* country_shr_1995
	duplicates drop
	reshape long share2_all_1995_, i(lse_id) j(country, string)
	ren share2_all_1995_ weight
	ren country_shr_1995 homecountry
	tempfile foreignweights_s1
	save `foreignweights_s1', replace
	


	* ---------------------- * 
	* a) Mean weights
	* ---------------------- * 

	
	foreach w_type in total foreign {
		*the files for this are created above and are tempfiles
		use ``w_type'weights_s1', clear
		gsort lse_id -weight
		by lse_id: gen weight_rank = _n
		ren country stat
		foreach rank in 1 2 3 4 { 
			expand 2 if weight_rank == `rank', gen(high_w)
			replace stat = "`rank'" if high_w == 1
			drop high_w
		}
		keep if inlist(stat, "1", "2", "3", "4", "US", "JP", "DE", "GB", "FR")
		sort lse_id stat
		foreach stat in 1 2 3 4 US JP DE GB FR {
			sleep 400 
			su weight if stat == "`stat'"
			sleep 1000
			texdoc local `stat'_`w_type'_1: display %9.2f r(mean)
			sleep 400
			di "`stat' `w_type' 1: `r(mean)'"
		}
	}
	

	* ---------------------- * 
	* b) HHI's of weights
	* ---------------------- * 

	* adapted from table_shocklevel_summary + build_bhj_data.do


		
	foreach w_type in total foreign {

		* country
		use ``w_type'weights_s1', clear
		collapse (mean) weight, by(country)
		egen HHI_sn = sum(weight^2)
		replace HHI_sn = HHI_sn * 100
		sleep 1000
		texdoc local HHI_sn_`w_type'_1 : display %9.1f HHI_sn[1]
		sleep 400
		di "`w_type' 1 HHI_sn: `HHI_sn_`w_type'_1"

		* country-year
		use ``w_type'weights_s1', clear
		expand 15
		bysort lse_id country: gen year = 1994 + _n
		gen s_nt = weight / 15
		drop weight
		collapse (mean) s_nt, by(country year)
		egen HHI_snt = sum(s_nt^2)
		replace HHI_snt = HHI_snt * 100
		sleep 1000
		texdoc local HHI_snt_`w_type'_1 : display %9.1f HHI_snt[1]
		sleep 1000
		di "`w_type' 1 HHI_snt: `HHI_snt[1]'"
	}


	texdoc local pwcorr_total_1 : display %9.2f .08159363
	sleep 1000
	texdoc local pwcorr_foreign_1: display %9.2f .12589684

	* ---------------------- * 
	* Output table
	* ---------------------- * 


	texdoc local notes "This table presents summary statistics for the country weights of firms. Columns 1--3 report statistics for the total weights. Column 4 presents information on foreign weights (normalized to 1). Columns 1 and 2 report the average weights of the largest countries. Columns 3 and 4 report the Herfindahl-Hirschman Index (HHI) at the country and country-year level. The mean pairwise correlation is the average pairwise correlation between any two firms (column 3) or firms within a home country (column 4)."
	texdoc stlog close
	include ${code_dir}/config/texdoctools.do
	tex `texpreamble'

	/*tex
	\begin{tabular}{@{}lS[table-format=1.3]lS[table-format=1.3]lS[table-format=1.3]S[table-format=1.3]@{}}
	\toprule
	\multicolumn{4}{c}{Average weights} & \multicolumn{3}{c}{Weight variation} \\
	\cmidrule(lr){1-4}\cmidrule(lr){5-7}
	& & & & & {Total} & {Foreign} \\

	& {(1)} & & {(2)} & & {(3)} & {(4)} \\
	\noalign{\vskip 0.8mm}
	\midrule
	\noalign{\vskip 0.8mm}

	Largest country & `1_total_1' &  United States &   `US_total_1' & & &  \\
	Second largest &  `2_total_1' &  Germany & `DE_total_1' & HHI country & `HHI_sn_total_1' \% & `HHI_sn_foreign_1'\% \\       
	Third largest &  `3_total_1'  &  Japan & `JP_total_1' & HHI country-year &  `HHI_snt_total_1' \% & `HHI_snt_foreign_1'\%  \\
	Fourth largest &  `4_total_1' &  France & `FR_total_1' & Mean pairwise corr. &    `pwcorr_total_1' &  `pwcorr_foreign_1'  \\

	\bottomrule
	\end{tabular}
	tex*/

	tex `texpostamble'
	texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl

local original "${log_dir}/table_4_firmweight_descriptives.log"
local new_location "${numb_dir}/table_4_firmweight_descriptives_numbers.log"
cap copy "`original'" "`new_location'", replace
